
$(function () {


    let list = null

    const list_info = {
        cat_one: 'all',
        cat_two: 'all',
        cat_three: 'all',
        sort_method: '不限',
        sort_type: 'ASC',
        current: 1,
        pagesize: 12
    }

    getCateOne()
    async function getCateOne(){

        const cat_one_list = await $.get('../php/getOne.php',null,null,'json')
    
        let str = `<li data-type="all" class="active">不限</li>`
        
        console.log(cat_one_list)
        cat_one_list.list.forEach(item => {
            str+=`<li data-type="${ item.cat_one_id }">${ item.cat_one_id }</li>`
        })
    
    $('.open > ul').html(str)
    
    
    }

    // 2-2. 渲染分页内容
    getTotalPage()
    async function getTotalPage() {

    const totalInfo = await $.get('../php/getPage.php', list_info, null, 'json')
    $('.pagination').pagination({
        pageCount: totalInfo.total,
        

        callback (index) {
          list_info.current = index.getCurrent()
          getGoodsList()
        }
      })

    }


    getGoodsList()
    async function getGoodsList () {

        const goodsList = await $.get('../php/getList.php',list_info,null,'json')

        console.log(goodsList)

        list = goodsList.list

        let str = ''

        goodsList.list.forEach(item => {

            str+=`
            <li>
                    <div class="imgBox">
                        <img src="${ item.goods_big_logo }" alt="">
                    </div>
                    <div class="center">
                            <h3 data-id="${ item.goods_id }">${ item.goods_name }</h3>
                            <p>销售价:
                                <span>
                                    ￥${ item.goods_price }
                                </span>
                            </p>
                    </div>
                    <div class="goods-actionq">
                        <div class="goods-buyq">
                            <p class="getBox" data-id="${ item.goods_id }">加入购物车</p>
                        </div>
                        <div class="goods-othersq">
                            <button>收藏</button>
                            <button>对比</button>
                        </div>
                    </div>
                </li>
            `
        })
        $('.gallery-listq > ul').html(str)
    }


    $('.open > ul').on('click', 'li', function () {
    
        const type = $(this).data('type')
    
        // list_info.cat_two = 'all'
        // list_info.cat_three = 'all'
        list_info.current = 1
    
        list_info.cat_one = type

        getTotalPage()
        getGoodsList()
        // $('.open > ul').html('<li data-type="all" class="active">不限</li>')
    
        // if (type === 'all') {
        //   $('.open > ul').html('<li data-type="all" class="active">全部</li>')
        // } else {
        //   getCateTwo()
        // }
      })

    $('.gallery-sortq').on('click', 'button', function () {

        const method = $(this).attr('data-method')
        const type = $(this).attr('data-type')

        $(this).addClass('active').siblings().removeClass('active')
        
        list_info.sort_method = method
        list_info.sort_type = type

        getTotalPage()
        getGoodsList()

        $(this)
        .attr('data-type', type === 'ASC' ? 'DESC' : 'ASC')
        .siblings()
        .attr('data-type', 'ASC')
  })

    $('.gallery-listq ul').on('click', 'h3', function () {

        console.log(123)
        const id = $(this).data('id')

        setCookie('goods_id', id)

        window.location.href = '../html/xiangqing.html'
  })

    $('.gallery-listq ul').on('click', '.getBox' , function() {

        // console.log(123)
        const cart = JSON.parse(window.localStorage.getItem('cart')) || []

        const id = $(this).data('id')
        // console.log(id)
        const flay = cart.some(item => item.goods_id == id)
        if(flay){
            const cart_goods = cart.filter(item => item.goods_id == id)[0]
            cart_goods.cart_number = cart_goods.cart_number - 0 + 1
        }else{
            const info = list.filter(item => item.goods_id == id)[0]

            info.cart_number = 1
            cart.push(info)
        }
        window.localStorage.setItem('cart',JSON.stringify(cart))
    })



})

